import 'dart:developer' as developer;
import 'package:flutter/foundation.dart';

/// LaraPress 日志处理类
/// 提供统一的日志输出接口，支持不同级别的日志记录
class LaraPressLogs {
  static LaraPressLogs? _instance;
  static LogLevel _currentLevel = LogLevel.info;
  static bool _enableConsoleOutput = true;
  static bool _enableDebugOutput = true;
  static String _appName = 'LaraPress';

  // 私有构造函数
  LaraPressLogs._();

  /// 获取单例实例
  static LaraPressLogs getInstance() {
    _instance ??= LaraPressLogs._();
    return _instance!;
  }

  /// 配置日志系统
  static void configure({
    LogLevel level = LogLevel.info,
    bool enableConsoleOutput = true,
    bool enableDebugOutput = true,
    String appName = 'LaraPress',
  }) {
    _currentLevel = level;
    _enableConsoleOutput = enableConsoleOutput;
    _enableDebugOutput = enableDebugOutput;
    _appName = appName;
  }

  /// 调试日志
  void debug(
    String message, {
    String? tag,
    Object? error,
    StackTrace? stackTrace,
  }) {
    _log(
      LogLevel.debug,
      message,
      tag: tag,
      error: error,
      stackTrace: stackTrace,
    );
  }

  /// 信息日志
  void info(
    String message, {
    String? tag,
    Object? error,
    StackTrace? stackTrace,
  }) {
    _log(
      LogLevel.info,
      message,
      tag: tag,
      error: error,
      stackTrace: stackTrace,
    );
  }

  /// 警告日志
  void warning(
    String message, {
    String? tag,
    Object? error,
    StackTrace? stackTrace,
  }) {
    _log(
      LogLevel.warning,
      message,
      tag: tag,
      error: error,
      stackTrace: stackTrace,
    );
  }

  /// 错误日志
  void error(
    String message, {
    String? tag,
    Object? error,
    StackTrace? stackTrace,
  }) {
    _log(
      LogLevel.error,
      message,
      tag: tag,
      error: error,
      stackTrace: stackTrace,
    );
  }

  /// 严重错误日志
  void fatal(
    String message, {
    String? tag,
    Object? error,
    StackTrace? stackTrace,
  }) {
    _log(
      LogLevel.fatal,
      message,
      tag: tag,
      error: error,
      stackTrace: stackTrace,
    );
  }

  /// 内部日志方法
  void _log(
    LogLevel level,
    String message, {
    String? tag,
    Object? error,
    StackTrace? stackTrace,
  }) {
    // 检查日志级别
    if (level.index < _currentLevel.index) {
      return;
    }

    // 格式化日志消息
    String formattedMessage = _formatMessage(level, message, tag: tag);

    // 控制台输出
    if (_enableConsoleOutput) {
      _outputToConsole(
        level,
        formattedMessage,
        error: error,
        stackTrace: stackTrace,
      );
    }

    // 调试输出
    if (_enableDebugOutput && kDebugMode) {
      _outputToDebug(
        level,
        formattedMessage,
        error: error,
        stackTrace: stackTrace,
      );
    }
  }

  /// 格式化日志消息
  String _formatMessage(LogLevel level, String message, {String? tag}) {
    DateTime now = DateTime.now();
    String timestamp =
        '${now.hour.toString().padLeft(2, '0')}:'
        '${now.minute.toString().padLeft(2, '0')}:'
        '${now.second.toString().padLeft(2, '0')}.'
        '${now.millisecond.toString().padLeft(3, '0')}';

    String levelStr = level.name.toUpperCase().padRight(7);
    String tagStr = tag != null ? '[$tag] ' : '';

    return '[$timestamp] $levelStr $tagStr$message';
  }

  /// 输出到控制台
  void _outputToConsole(
    LogLevel level,
    String message, {
    Object? error,
    StackTrace? stackTrace,
  }) {
    switch (level) {
      case LogLevel.debug:
        print('🐛 $message');
        break;
      case LogLevel.info:
        print('ℹ️  $message');
        break;
      case LogLevel.warning:
        print('⚠️  $message');
        break;
      case LogLevel.error:
        print('❌ $message');
        break;
      case LogLevel.fatal:
        print('💀 $message');
        break;
    }

    if (error != null) {
      print('   错误详情: $error');
    }

    if (stackTrace != null) {
      print('   堆栈跟踪: $stackTrace');
    }
  }

  /// 输出到调试器
  void _outputToDebug(
    LogLevel level,
    String message, {
    Object? error,
    StackTrace? stackTrace,
  }) {
    developer.log(
      message,
      name: '$_appName.${level.name}',
      level: _getLogLevelValue(level),
      error: error,
      stackTrace: stackTrace,
    );
  }

  /// 获取日志级别对应的数值
  int _getLogLevelValue(LogLevel level) {
    switch (level) {
      case LogLevel.debug:
        return 500;
      case LogLevel.info:
        return 800;
      case LogLevel.warning:
        return 900;
      case LogLevel.error:
        return 1000;
      case LogLevel.fatal:
        return 1200;
    }
  }

  /// 设置日志级别
  static void setLevel(LogLevel level) {
    _currentLevel = level;
  }

  /// 启用/禁用控制台输出
  static void setConsoleOutput(bool enabled) {
    _enableConsoleOutput = enabled;
  }

  /// 启用/禁用调试输出
  static void setDebugOutput(bool enabled) {
    _enableDebugOutput = enabled;
  }

  /// 获取当前配置
  static Map<String, dynamic> getConfig() {
    return {
      'level': _currentLevel.name,
      'consoleOutput': _enableConsoleOutput,
      'debugOutput': _enableDebugOutput,
      'appName': _appName,
    };
  }
}

/// 日志级别枚举
enum LogLevel { debug, info, warning, error, fatal }

/// 全局日志实例
final LaraPressLogs logs = LaraPressLogs.getInstance();
